library(tidyverse)

DATA         <- tribble(
  ~SECTION                   , ~LABELY         , ~GROUP                    , ~PE      , ~CILO    , ~CIHI    , 
  "Rated immigrants 0 to 25" , "Rated 0 to 25" , "Ratings about immigrants", 0.0841323, 0.0783298, 0.0903225, 
  "Rated immigrants 26 to 49", "Rated 26 to 49", "Ratings about immigrants", 0.103547 , 0.0972713, 0.1101782, 
  "Rated immigrants at 50"   , "Rated at 50"   , "Ratings about immigrants", 0.0899263, 0.0842756, 0.0959161, 
  "Did not rate immigrants"  , "Did not rate"  , "Ratings about immigrants", 0.0556298, 0.0505453, 0.0611927, 
  "Rated immigrants 51 to 75", "Rated 51 to 75", "Ratings about immigrants", 0.2576972, 0.248924 , 0.2666698, 
  "Rated immigrants 76 to 99", "Rated 76 to 99", "Ratings about immigrants", 0.3314564, 0.3221214, 0.3409259, 
  "Rated immigrants at 100"  , "Rated at 100"  , "Ratings about immigrants", 0.077611 , 0.0722419, 0.0833432, 
  "Rated Whites 0 to 25"     , "Rated 0 to 25" , "Ratings about Whites"    , 0.0550068, 0.0500829, 0.0603839, 
  "Rated Whites 26 to 49"    , "Rated 26 to 49", "Ratings about Whites"    , 0.1078019, 0.101311 , 0.1146556, 
  "Rated Whites at 50"       , "Rated at 50"   , "Ratings about Whites"    , 0.1073693, 0.1011499, 0.1139227, 
  "Did not rate Whites"      , "Did not rate"  , "Ratings about Whites"    , 0.0389995, 0.0346414, 0.0438811, 
  "Rated Whites 51 to 75"    , "Rated 51 to 75", "Ratings about Whites"    , 0.2507287, 0.2419346, 0.2597329, 
  "Rated Whites 76 to 99"    , "Rated 76 to 99", "Ratings about Whites"    , 0.3612866, 0.3517793, 0.3709038, 
  "Rated Whites at 100"      , "Rated at 100"  , "Ratings about Whites"    , 0.0788072, 0.0734955, 0.0844677, 
  "Rated Blacks 0 to 25"     , "Rated 0 to 25" , "Ratings about Blacks"    , 0.0562365, 0.051386 , 0.061515 , 
  "Rated Blacks 26 to 49"    , "Rated 26 to 49", "Ratings about Blacks"    , 0.0807883, 0.0751442, 0.0868165, 
  "Rated Blacks at 50"       , "Rated at 50"   , "Ratings about Blacks"    , 0.0846467, 0.0791692, 0.0904659, 
  "Did not rate Blacks"      , "Did not rate"  , "Ratings about Blacks"    , 0.0383333, 0.0341366, 0.043023 , 
  "Rated Blacks 51 to 75"    , "Rated 51 to 75", "Ratings about Blacks"    , 0.2385611, 0.2299606, 0.24738  , 
  "Rated Blacks 76 to 99"    , "Rated 76 to 99", "Ratings about Blacks"    , 0.3862839, 0.3764971, 0.3961635, 
  "Rated Blacks at 100"      , "Rated at 100"  , "Ratings about Blacks"    , 0.1151502, 0.1087826, 0.1218396, 
  "Rated Hispanics 0 to 25"  , "Rated 0 to 25" , "Ratings about Hispanics" , 0.0427693, 0.0386793, 0.0472704, 
  "Rated Hispanics 26 to 49" , "Rated 26 to 49", "Ratings about Hispanics" , 0.0754557, 0.0700574, 0.0812336, 
  "Rated Hispanics at 50"    , "Rated at 50"   , "Ratings about Hispanics" , 0.085916 , 0.0803792, 0.0917961, 
  "Did not rate Hispanics"   , "Did not rate"  , "Ratings about Hispanics" , 0.0513694, 0.0464916, 0.0567287, 
  "Rated Hispanics 51 to 75" , "Rated 51 to 75", "Ratings about Hispanics" , 0.2536452, 0.2448402, 0.2626567, 
  "Rated Hispanics 76 to 99" , "Rated 76 to 99", "Ratings about Hispanics" , 0.3841637, 0.3743918, 0.39403  , 
  "Rated Hispanics at 100"   , "Rated at 100"  , "Ratings about Hispanics" , 0.1066808, 0.1004705, 0.1132266, 
  "Rated Asians 0 to 25"     , "Rated 0 to 25" , "Ratings about Asians"    , 0.0403364, 0.0359748, 0.0452021, 
  "Rated Asians 26 to 49"    , "Rated 26 to 49", "Ratings about Asians"    , 0.0738372, 0.0682307, 0.0798649, 
  "Rated Asians at 50"       , "Rated at 50"   , "Ratings about Asians"    , 0.0902615, 0.0846484, 0.0962078, 
  "Did not rate Asians"      , "Did not rate"  , "Ratings about Asians"    , 0.0593976, 0.0542042, 0.0650544, 
  "Rated Asians 51 to 75"    , "Rated 51 to 75", "Ratings about Asians"    , 0.2452713, 0.2364795, 0.2542812, 
  "Rated Asians 76 to 99"    , "Rated 76 to 99", "Ratings about Asians"    , 0.3912962, 0.3815678, 0.4011117, 
  "Rated Asians at 100"      , "Rated at 100"  , "Ratings about Asians"    , 0.0995997, 0.093732 , 0.1057919, 
  
)

DATA$GROUP   <- factor(DATA$GROUP  , levels = unique(DATA$GROUP))
DATA$SECTION <- factor(DATA$SECTION, levels = rev(unique(DATA$SECTION)))
LABELS       <- c("Ratings about immigrants" = "Ratings\nabout\nimmigrants", "Ratings about Whites" = "Ratings\nabout\nWhites", "Ratings about Blacks" = "Ratings\nabout\nBlacks", "Ratings about Hispanics" = "Ratings\nabout\nHispanics", "Ratings about Asians" = "Ratings\nabout\nAsians")

COLOR1 <- "darkseagreen4"
COLOR2 <- "darkseagreen3"
COLOR3 <- "darkseagreen1"
COLOR4 <- "white"
COLOR5 <- "gray90"
COLOR6 <- "indianred2"
COLOR7 <- "indianred4"

theme.z <-   theme(
  axis.text.x        = element_text(size = 15, color = "black", hjust = 0.5, margin = margin(t = 8, b = 0)),
  axis.text.x.top    = element_text(size = 15, color = "black", hjust = 0.5, margin = margin(t = 8, b = 0)),
  axis.text.y        = element_text(size = 15, color = "black", hjust = 1  , margin = margin(l = 8, r = 8)),
  axis.text.y.right  = element_text(size = 15, color = "black", hjust = 0  , margin = margin(l = 8, r = 8)),
  axis.ticks.x       = element_blank(),
  axis.ticks.y       = element_blank(),
  axis.title.x       = element_blank(), 
  axis.title.y       = element_blank(),
  legend.position    = "none",
  panel.background   = element_blank(), 
  panel.border       = element_blank(), 
  panel.grid.major.x = element_blank(), 
  panel.grid.major.y = element_blank(),
  panel.grid.minor.x = element_blank(), 
  panel.grid.minor.y = element_blank(),
  panel.spacing.x    = unit(2, "lines"),
  panel.spacing.y    = unit(1, "lines"),
  plot.background    = element_rect(fill = "white"),
  plot.caption       = element_text(size = 12 , hjust = 0  , margin = margin(t = 10)),
  plot.margin        = unit(c(t = 10,r = 5,b = 10,l = 5),"pt"),
  plot.subtitle      = element_text(size = 15 , hjust = 0.5, margin = margin(b = 10)),
  plot.title         = element_text(size = 17 , hjust = 0.5, margin = margin(t = 0, b = 10), face = "bold"),
)

ggplot(DATA, aes(fill = SECTION, y = 100*PE, x = GROUP)) +
  geom_col(color = "black", linewidth = 1.2, width = 1) +
  geom_rect(aes(xmin = 0.5, xmax = 5.5, ymin = 0, ymax = 100), fill = NA, col = "black", linewidth = 2.5) +
  scale_fill_manual(values = rep.int(c(COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7), 8)) +
  scale_y_continuous(limits = c(0,100.1), position = "left", breaks = c(4.2,13.6,23.3,30.5,46.2,75.7,96.1), label = unique(DATA$LABELY), expand = c(0,0), sec.axis = sec_axis(~.,breaks = seq(0,100,10), label = c("\u2014", rep.int("\u2013",4),"\u2014", rep.int("\u2013",4),"\u2014"))) +
  scale_x_discrete(labels = LABELS, expand = c(0,0)) +
  theme.z + 
  theme(plot.margin = unit(c(t = 10, r = 5, b = 15, l = 5), "pt"))

ggsave(file = "Figure 1.eps", width = 8, height = 6)
ggsave(file = "Figure 1.svg", width = 8, height = 6)
